package com.oss.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.appengine.api.memcache.stdimpl.GCacheFactory;
import com.oss.model.Site;
import com.oss.model.Link;
import com.oss.model.Project;
import com.oss.model.Tag;
import com.oss.model.Topic;
import com.oss.service.SiteService;
import com.oss.service.LinkService;
import com.oss.service.ProjectService;
import com.oss.service.TagService;
import com.oss.service.TopicService;
import com.oss.web.BaseController;

/**
 * 系统缓存服务：使用了静态成员cache对外提供服务
 * 
 * @author xueqiang.mi
 * @since 2010-7-25
 */
@SuppressWarnings("unchecked")
public class CacheService {

	static public final String KEY_ROOT_TAG = "rootTags";
	static public final String KEY_CHILD_TAG = "childTags";
	static public final String KEY_PRJ = "prjs";
	static public final String KEY_TOPIC = "topics";

	static public final String INDEX_NEWS = "news";
	static public final String INDEX_LATEST_LINKS = "latestLinks";
	static public final String INDEX_HOTTEST_TOPICS = "hottestTopics";
	static public final String INDEX_LATEST_SITES = "latestSites";

	static public Cache cache;

	static {
		try {
			CacheFactory cacheFactory = CacheManager.getInstance()
					.getCacheFactory();
			Map props = new HashMap();
			props.put(GCacheFactory.EXPIRATION_DELTA, 3600 * 12);
			cache = cacheFactory.createCache(props);
		} catch (CacheException e) {
			e.printStackTrace();
		}
	}

	static public List<Tag> getChildTags() {
		return (List<Tag>) cache.get(KEY_CHILD_TAG);
	}

	static public List<Project> getPrjs() {
		return (List<Project>) cache.get(KEY_PRJ);
	}

	static public List<Tag> getRootTags() {
		return (List<Tag>) cache.get(KEY_ROOT_TAG);
	}

	static public List<Topic> getTopics() {
		return (List<Topic>) cache.get(KEY_TOPIC);
	}

	@Autowired
	protected LinkService lnkService;
	@Autowired
	protected ProjectService prjService;
	@Autowired
	protected SiteService siteService;
	@Autowired
	protected TagService tagService;
	@Autowired
	protected TopicService topicService;

	public List<Topic> getHottestTopics() {
		return (List<Topic>) cache.get(INDEX_HOTTEST_TOPICS);
	}

	public List<Site> getLatestBlogs() {
		return (List<Site>) cache.get(INDEX_LATEST_SITES);
	}

	public List<Link> getLatestLinks() {
		return (List<Link>) cache.get(INDEX_LATEST_LINKS);
	}

	public List<Link> getNews() {
		return (List<Link>) cache.get(INDEX_NEWS);
	}

	/**
	 * 初始化缓存
	 */
	public void refresh() {
		// 清空缓存
		if (!cache.isEmpty()) {
			cache.clear();
		}
		refreshCache();
		refreshIndex();
	}

	/**
	 * 重置缓存静态数据
	 */
	public void refreshCache() {
		// 读取Tag树
		cache.put(KEY_ROOT_TAG, tagService.getTagTree());
		// 读取所有子tag放入缓存
		cache.put(KEY_CHILD_TAG, tagService.getChildTags());

		// 读取所有project放入缓存
		List<Project> prjs = new ArrayList<Project>();
		for (Tag tag : tagService.getChildTags()) {
			List<Project> list = prjService.getPrjsCachedByTag(tag.getId(), 1,
					500);
			prjs.addAll(list);
		}
		cache.put(KEY_PRJ, prjs);

		// 读取所有topic放入缓存
		List<Topic> topics = topicService.getTopics(1, 100);
		cache.put(KEY_TOPIC, topics);
	}

	/**
	 * 重置缓存首页数据
	 */
	public void refreshIndex() {
		// 读取最新News放入缓存
		// cache.put(INDEX_NEWS,
		// lnkService.getNews(BaseController.DEFAULT_INDEX,
		// BaseController.DEFAULT_SMALL_SIZE));
		// 读取最新Link放入缓存
		cache.put(INDEX_LATEST_LINKS, lnkService.getLatestLnks(
				BaseController.DEFAULT_INDEX, BaseController.LNK_MEDIUM_SIZE));
		// 读取最热Topic放入缓存
		cache.put(INDEX_HOTTEST_TOPICS, topicService.getHottestTopics(
				BaseController.DEFAULT_INDEX, BaseController.PRJ_MEDIUM_SIZE));
		// 读取最新Site放入缓存
		cache.put(INDEX_LATEST_SITES, siteService.getLatestSites(
				BaseController.DEFAULT_INDEX, BaseController.PRJ_MEDIUM_SIZE));
	}
}
